home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / misc / Gfx4PCQ.lha / WindowLib / Examples / WindowDemo / WindowDemo.p < prev    next >
Encoding:
Text File  |  1997-07-19  |  10.8 KB  |  350 lines

  1. Program WindowDemo;
  2. {This is a program that demonstrates some features of THOR's windowlib.
  3.  For more information on how this works read the include file
  4.  utils/windowlib.i
  5.  © 1997 THOR - Software}
  6.  
  7. {Include some useful stuff, and the windowlib itself}
  8. {$I "include:utils/windowlib.i"}
  9. {$I "include:utils/stringlib.i"}
  10.   
  11. {This is the demonstration part...}
  12. PROCEDURE Demo;
  13.     
  14. CONST
  15.     {Setup a menu bar in an simple way. The menu is created out of them.
  16.      The entries in this array are of a structure called MenuCommand,
  17.      each of them describing one menu item, a menu bar or controlling
  18.      the generation. The structure itself consists of:
  19.         1) the type of entry to be created:
  20.             a MC_MENU (menubar), an MC_ITEM (an item of the menu)
  21.             or an MC_SUBITEM (subitem of an item).
  22.             The last item or subitem of a menu must be a
  23.             MC_LASTSUBITEM/MC_LASTITEM or MC_LASTMENU resp.
  24.             to tell windowlib that one element of the menu is
  25.             now complete.
  26.         2)    some flags describing how this should look like.
  27.             They are either MC_NORMALMENU (ordinary menu bar)
  28.             or MC_NORMAL for a select-action menu item, or
  29.             MC_CHECKABLE to get a menu item that can be turned
  30.             on or off. MC_BAR is special in the sense that it
  31.             creates a separation bar.
  32.         3)    One character consisting of a shortcut (Amiga + key)
  33.         4)    A string to be appear in the menu itself.}    
  34.         
  35.     menubar : ARRAY[1..8] OF MenuCommand=(
  36.         (MC_MENU,MC_NORMALMENU,'\0',0,"Menu 1"),
  37.          (MC_ITEM,MC_NORMAL,'\0',0,"Item 1"),
  38.          (MC_ITEM,MC_NORMAL,'\0',0,"Item 2"),
  39.          (MC_ITEM,MC_CHECKABLE,'\0',0,"Onoff"),
  40.          (MC_ITEM,MC_BAR,'\0',0,""),
  41.          (MC_ITEM,MC_NORMAL,'Q',0,"Exit"),
  42.          (MC_LASTITEM,0,'\0',0,""),
  43.         (MC_LASTMENU,0,'\0',0,""));
  44.         
  45. VAR
  46.     x,y    :    SHORT;
  47.     i    :    SHORT;
  48.     s    :    ScreenPtr;
  49.     w    :    WindowPtr;
  50.     g1,g2,g3,g4,g5:    GadgetPtr;
  51.     t    :    INTEGER;
  52.     b    :    BOOLEAN;
  53.     buf    :    ARRAY[0..15] OF CHAR;
  54.     ex    :    BOOLEAN;
  55.     menu,item,subitem : SHORT;
  56.  
  57.     
  58. BEGIN
  59.     InitGraphics;        {setup gfx system}
  60.  
  61.     {open a custom screen. Arguments are:
  62.         left and top edge of the screen, width, height and
  63.         depth of bitplanes (2^depth=# of colors)
  64.         the monitor ID - here in HIRES
  65.         and a title}
  66.     s:=OpenAScreen(0,0,640,300,2,MON_HIRES,"Test screen");
  67.     IF s<>NIL THEN BEGIN
  68.  
  69.     {choose the pen 3 to be fire-brigade red. Arguments are
  70.      the screen, the number of the pen and the red,green and blue
  71.      components of the new color, on a range from 0 (min) to 15 (max).
  72.      The windowlib does not support 16 bit colors...}
  73.     SetColor(s,3,15,0,0);
  74.     {please note that this is bad style since we haven't tested if the
  75.      screen did open. Sigh}
  76.         
  77.     {open a window on this screen. The arguments are
  78.      the screen, the position (left/top coordinates) the
  79.      width and heights and some flags which should be left to
  80.      14 for almost all reasons, and a string giving the title.}
  81.     w:=OpenScreenWindow(s,0,20,640,240,2+4+8,"A Test");
  82.     
  83.     IF w<>NIL THEN BEGIN
  84.         {setup a font to be used in this window. In this case,
  85.          it's topaz.9. Choose something better if you like...}
  86.         b:=SetWindowFont(w,"topaz.font",9);
  87.  
  88.         SetStyle(w,4);    {Set the style for the text to be printed:
  89.             1 is underlined, 2 is bold, 4 is italic.
  90.             Add the values for style combinations}
  91.  
  92.         Color(w,1);    {chose a pen to be used}
  93.  
  94.         {draw a nifty graphics}        
  95.         FOR i:=0 TO 10 DO BEGIN
  96.             Plot(w,0,i*24);        {plot a point}
  97.             DrawTo(w,i*64,240);    {draw a line from it to this position}
  98.         END;
  99.     
  100.         {wait until the user clicks into the window}
  101.         WaitForClick(w);
  102.  
  103.         {clear the window}
  104.         ClearWindow(w);
  105.  
  106.         {draw an ellipse. Arguments are the center and the two
  107.          radii}
  108.         Ellipse(w,320,120,300,150);
  109.  
  110.         {fill it in a different color}
  111.         Color(w,2);
  112.         Fill(w,320,120);
  113.     
  114.  
  115.         {wait until the user clicks into the window}
  116.         WaitForClick(w);
  117.         ClearWindow(w);        {clear it}
  118.     
  119.         Color(w,1);        {choose pen}
  120.         {another nice gfx with ellipses}    
  121.         FOR i:=0 TO 10 DO
  122.             Ellipse(w,320,120,i*20,100-i*10);        
  123.  
  124.         WaitForClick(w);
  125.         ClearWindow(w);        {wait and clear}
  126.  
  127.         {turn on boundary drawing. This affects filled boxes
  128.          and ellipses.}
  129.         Boundary(w,TRUE);
  130.         OlColor(w,1);            {choose the pen for the boundary}
  131.         FOR i:=0 TO 10 DO BEGIN
  132.             Color(w,i);        {choose the pen for the interiour}
  133.             PBox(w,i*32,i*12,640-i*32,240-i*12);    {draw a filled, framed rectangle}
  134.         END;
  135.     
  136.         Boundary(w,FALSE);        {turn off boundary drawing again}
  137.         WaitForClick(w);        {wait until the user clicks into the window}
  138.         ClearWindow(w);            {clear it}
  139.     
  140.         {draw the same, but not filled}
  141.         FOR i:=0 TO 10 DO BEGIN
  142.             Color(w,i);        {chose color}
  143.             Box(w,i*32,i*12,640-i*32,240-i*12);    {draw a frame}
  144.         END;
  145.  
  146.         WaitForClick(w);
  147.         ClearWindow(w);        {wait and clear}
  148.  
  149.         Boundary(w,TRUE);    {again boundary mode}    
  150.         OlColor(w,1);        {color for the boundary}    
  151.     
  152.         FOR i:=0 TO 10 DO BEGIN
  153.             Color(w,i);    {color for the interiour}
  154.             PEllipse(w,320,120,i*20,100-i*10);    {draw a filled, framed ellipse}        
  155.         END;
  156.  
  157.         {some interactive selection functions}
  158.  
  159.         {let the user select one point of the window with a cross}
  160.         SelectPoint(w,x,y);
  161.  
  162.         {let the user select a region of the window by "dragging" it}
  163.         DragBox(w,x,y,x,y);    
  164.  
  165.         {we do actually nothing with them, just for demonstration}
  166.  
  167.  
  168.         {tell the windowlib we want to hear if the user presses a
  169.          mouse button}        
  170.         RequestStart(w,MOUSEBUTTONS_F);
  171.         REPEAT
  172.             Mouse(w,x,y);    {read the mouse position}
  173.             Plot(w,x,y);    {plot a point at this position}
  174.         UNTIL NextRequest(w)=MOUSEBUTTONS_F;    {until the mouse button gets pressed}
  175.         {turn off notification}
  176.         RequestEnd(w,MOUSEBUTTONS_F);
  177.             
  178.         {create some gadgets. A text button is just a button with
  179.          some text in it. It is called a "bool gadget" in intution
  180.          terms. The user can press it to force some action.
  181.          Arguments are: the window, the x and y coordinates of its
  182.          position, the width and height - beeing zero here to tell
  183.          the windowlib to calculate them by the text they should
  184.          contain and the text itself}
  185.         g1:=CreateTextButton(w,4,04,0,0,"Demo gadget");
  186.         g2:=CreateTextButton(w,4,20,0,0,"Another one");
  187.  
  188.         {create a string field: This is a rectangular area where
  189.          the user may insert text. These are called "string gadgets"
  190.          by the intuition part of the OS.
  191.          The arguments are the window, the position x and y and the
  192.          width and height. The height is set to zero to tell window
  193.          lib to fiddle this out by itself - the window's font is
  194.          used here.}
  195.         g3:=CreateStringField(w,4,40,100,0);
  196.  
  197.         {create a slider. They are called "prop gadgets" in intuition
  198.          language and let users select a choise out of a list of
  199.          items. Arguments are the window, the position x,y, the
  200.          width and height, and a flag beeing FALSE for horizontal and
  201.          TRUE for vertical movement. The size of the list = the number
  202.          of choices is setup later.}
  203.         g4:=CreateSlider(w,4,60,100,8,FALSE);
  204.  
  205.         {create a toggle gadget. This is also a "bool gadget" in
  206.          intuition notation. This is a gadget that can be toggled
  207.          "on" or "off" by the user by pressing it.}
  208.         g5:=CreateTextToggle(w,4,80,0,0,"Toggle");
  209.  
  210.         {note that we did some bad style above since we never
  211.          checked if the gadgets could have been created}    
  212.  
  213.         {create a menu and attach it to the window. The definition
  214.          of the menu is done by a structure you see on top of this
  215.          procedure}
  216.         CreateMenu(w,@menubar);
  217.  
  218.         {setup the slider g4 to describe a list of 16 items (=16
  219.          possible choices), one beeing visible at a time, with 0
  220.          beeing the initial choice. The choices returned by window-
  221.          lib are numbered from 0 to 16}
  222.         SetSlider(g4,16,1,0);
  223.  
  224.         {display the cursor in the string field created on top
  225.          and let the user enter something}
  226.         b:=ActivateField(g3);
  227.  
  228.  
  229.         {tell the windowlib we want to get informed if:
  230.             the user wants to close the window
  231.             the user releases a gadget,i.e. the sliders, the buttons
  232.             or the text field
  233.             the user presses a mouse button.
  234.             the user selected a menu.
  235.             the user pressed a key}        
  236.         RequestStart(w,CLOSEWINDOW_f OR GADGETUP_f OR MOUSEBUTTONS_F OR MENUPICK_f OR RAWKEY_f);
  237.  
  238.         Color(w,1);    {choose the pen}
  239.         ex:=FALSE;    {flag if we should exit}
  240.         REPEAT
  241.             {if the mouse button is pressed, read the
  242.              mouse position and draw a point at it.
  243.              read the next event in this case}
  244.             IF MouseButton(w) THEN BEGIN
  245.                 Mouse(w,x,y);
  246.                 Plot(w,x,y);
  247.                 t:=NextRequest(w);
  248.             END ELSE BEGIN
  249.                 {else redraw the gadgets since the
  250.                  user might have drawn over them}
  251.                 RefreshButton(g1);
  252.                 RefreshButton(g2);
  253.                 RefreshButton(g3);
  254.                 RefreshButton(g4);
  255.                 RefreshButton(g5);
  256.                 {and wait for something to be happen}
  257.                 t:=WaitRequest(w);
  258.             END;
  259.                 
  260.             {now look what has happend, if at all}
  261.  
  262.             IF t=GADGETUP_f THEN BEGIN
  263.                 {a gadget has been released}
  264.                 
  265.                 {setup a drawing position}
  266.                 Position(w,50,120);
  267.  
  268.                 {get the number of the gadget that has been
  269.                  pressed. They are numbered from one
  270.                  in increasing order of their creation}
  271.                 CASE LastGadgetID(w) OF
  272.                 1:    {draw some text if one of the buttons has been released}
  273.                     DrawText(w,"Gadget 1");    
  274.                 2:
  275.                     DrawText(w,"Gadget 2");
  276.                 3:    {get the contents of the string field and draw it}
  277.                     DrawText(w,BufferFromField(g3));
  278.                 4:    BEGIN    {read the position of the slider, convert it to text and draw it}
  279.                         t:=IntToStr(@buf,FirstFromSlider(g4));
  280.                         DrawText(w,@buf);
  281.                     END;
  282.                 5:    BEGIN    {the on/off gadget has changed its state. Read it and display it}
  283.                         IF GetToggle(g5) THEN BEGIN
  284.                             DrawText(w,"On ");
  285.                         END ELSE
  286.                             DrawText(w,"Off");
  287.                     END;
  288.                 END;
  289.             END;
  290.             IF t=CLOSEWINDOW_f THEN
  291.                 {if we got the message that the user wants to
  292.                  shutdown, set a flag}
  293.                 ex:=TRUE;
  294.             IF t=RAWKEY_f THEN BEGIN
  295.                 {read the key that has been pressed into a buffer,
  296.                  and print this buffer.}
  297.                 LastKey(w,@buf,i);
  298.                 Position(w,50,90);
  299.                 DrawText(w,@buf);
  300.             END;
  301.             IF t=MENUPICK_f THEN BEGIN
  302.                 {a menu has been selected, read which one.
  303.                  The number returned is the menu number, the item
  304.                  number and the subitem number resp.
  305.                   They are numbered from 0 increasing in the
  306.                  order in the menu bar or menu. The item or
  307.                  subitem is -1 if it has not been selected}
  308.                 LastMenu(w,menu,item,subitem);
  309.                 IF item=4 THEN
  310.                     ex:=TRUE;    {"exit" choosen?}
  311.                 IF item=2 THEN BEGIN
  312.                     {check the state of the checkmark item.
  313.                      The numbers given here are again the
  314.                      number of the menu, of the item and of
  315.                      the subitem as described above. -1 means
  316.                      that this item has no subitem}                
  317.                     IF CheckMarkOfMenu(w,0,2,-1) THEN BEGIN
  318.                         {enable the menu item #1, i.e.
  319.                          make it selectable}
  320.                         OnMenuPoint(w,0,1,-1);
  321.                     END ELSE
  322.                         {disable menu item #1 in the first
  323.                          menu, i.e. forbid selection}
  324.                         OffMenuPoint(w,0,1,-1);
  325.                     {set the state of the toggle gadget by the
  326.                      state of this gadget}
  327.                     SetToggle(g5,CheckMarkOfMenu(w,0,2,-1));
  328.                 END;
  329.             END;
  330.         UNTIL ex;        {repeat until the user had enough}
  331.  
  332.         {tell windowlib we no longer want to receive messages
  333.          from this window}
  334.         RequestEnd(w,CLOSEWINDOW_f OR GADGETUP_f);
  335.     
  336.         {close this window}
  337.         CloseAWindow(w);
  338.     END;
  339.     {close the screen}
  340.     CloseAScreen(s);
  341.     END;
  342.     {quit the gfx system}
  343.     ExitGraphics;
  344. END;
  345.  
  346. {a tiny main program}
  347. BEGIN
  348.     Demo;
  349. END.
  350.